﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:fn="http://www.w3.org/2005/xpath-functions">
<!--
程序名:表结构生成工具
版本号    修改者    修改时间    修改描述
1.0       liuleyi   20141230    创建此程序
-->
	<xsl:output method="text" indent="no" encoding="utf-8" byte-order-mark="no"/>  
	<xsl:template match="/">
<xsl:for-each select="Tables">
	<xsl:variable name="StringTypeDefine" select="document('../model/0101_StringTypeDefine.xml')"/>
	<xsl:variable name="EnumTypeDefine" select="document('../model/0102_EnumTypeDefine.xml')"/>
	<xsl:variable name="BooleanTypeDefine" select="document('../model/0103_BooleanTypeDefine.xml')"/>
	<xsl:variable name="DateTypeDefine" select="document('../model/0104_DateTypeDefine.xml')"/>
	<xsl:variable name="NumberTypeDefine" select="document('../model/0105_NumberTypeDefine.xml')"/>
	<xsl:variable name="IntegerTypeDefine" select="document('../model/0106_IntegerTypeDefine.xml')"/>
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" /> 
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" /> 

<xsl:for-each select="TDTable">
<xsl:variable name="fileName" select="concat('./generate/mysql/sql/table/',@code,'.sql')"/>
<xsl:result-document encoding="utf-8" method="text" href="{$fileName}"> 
			<xsl:variable name="TDTableXmlFile" select="concat('../model/tables/TDtable/',@code,'.xml')"/>
			<xsl:variable name="TDTableXml" select="document($TDTableXmlFile)"/>
			<xsl:for-each select="$TDTableXml/Table">
<xsl:variable name="TableName" select="@code"/>
--建表开始...
--***********************************************************************&#x0D;&#x0A;
--**表名 <xsl:value-of select="@label"/>
--**建表时间 <xsl:value-of select="current-dateTime()"/>
--***********************************************************************&#x0D;&#x0A;
CREATE TABLE <xsl:value-of select="lower-case($TableName)"/>
(
<xsl:for-each select="Column">  <xsl:value-of select="@name"/>
<xsl:variable name="typeTable" select="@name"/>
<xsl:variable name="require" select="@require"/>
<xsl:for-each select="$StringTypeDefine//String[@typename=$typeTable]"> VARCHAR(<xsl:value-of select="@length"/>)<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:for-each select="$EnumTypeDefine//Enum[@typename=$typeTable]"> VARCHAR(20)<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:for-each select="$BooleanTypeDefine//Boolean[@typename=$typeTable]"> VARCHAR(1)<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:for-each select="$DateTypeDefine//Date[@typename=$typeTable]"> DATETIME<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:for-each select="$NumberTypeDefine//Number[@typename=$typeTable]"> NUMERIC(<xsl:value-of select="@precision"/>)<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:for-each select="$IntegerTypeDefine//Integer[@typename=$typeTable]"> Integer<xsl:if test="$require='Y'"> NOT NULL</xsl:if>
</xsl:for-each>
<xsl:if test="position()!=last()">,
</xsl:if>
</xsl:for-each>
);

ALTER TABLE <xsl:value-of select="$TableName"/> COMMENT = '<xsl:value-of select="@label"/>';

<xsl:for-each select="Constraint"> 
<xsl:variable name="constraintName" select="@name"/>
<xsl:variable name="constraintSpace" select="@space"/>
<xsl:variable name="constraintType" select="@type"/>
ALTER TABLE <xsl:value-of select="$TableName"/>
<xsl:if test="$constraintType='primary'">
  ADD CONSTRAINT <xsl:value-of select="lower-case($constraintName)"/> PRIMARY KEY (
<xsl:for-each select="Column">
 <xsl:value-of select="lower-case(@name)"/><xsl:if test="position()!=last()">,</xsl:if>
</xsl:for-each>
);
  </xsl:if>
</xsl:for-each>
<xsl:for-each select="Index"> 
 <xsl:variable name="indexName" select="@name"/>
 <xsl:variable name="indexSpace" select="@space"/>
ALTER TABLE <xsl:value-of select="$TableName"/> ADD INDEX <xsl:value-of select="lower-case($indexName)"/> (
 <xsl:for-each select="Column">
 <xsl:value-of select="lower-case(@name)"/><xsl:if test="position()!=last()">,</xsl:if>
 </xsl:for-each>
 );
 </xsl:for-each>
/
</xsl:for-each>
</xsl:result-document>
</xsl:for-each>

</xsl:for-each>
	</xsl:template>
</xsl:stylesheet>